<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/29
 * Time: 11:49
 */
namespace Admin\Controller;
use Admin\Controller\CommonController;

class OrderController extends CommonController{
    public function index(){
    $count      = M("order")->count();// 查询满足要求的总记录数
    $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
    $Page->setConfig('prev' , '上一页');
    $Page->setConfig('next', '下一页');
    $Page->setConfig('first', '第一页');
    $Page->setConfig('last', '尾页');
    $show       = $Page->show();// 分页显示输出
    $orderdata = M("order")->alias("o")
        ->join("tb_channel c on c.id=o.channel_id")
        ->join("tb_recipients r on r.id=o.recipients_id")
        ->join("tb_sender s on s.id=o.sender_id")
        ->join("tb_country co on co.id=r.country_id")
        ->join("tb_order_goods og on og.order_id=o.id")
        ->join("tb_goods g on g.id=og.goods_id")
        ->join("tb_users u on u.id=o.users_id")
        ->limit($Page->firstRow.','.$Page->listRows)
        ->order("o.ord_status,o.cancel asc,o.ord_time desc")->where("o.cancel=0")->select();
    $this->assign("page",$show);
    $this->assign("data",$orderdata);
    $this->display();

}
    public function custom_service(){
        $count      = M("order")->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('prev' , '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('first', '第一页');
        $Page->setConfig('last', '尾页');
        $show       = $Page->show();// 分页显示输出
        $orderdata = M("order")->alias("o")
            ->join("tb_channel c on c.id=o.channel_id")
            ->join("tb_recipients r on r.id=o.recipients_id")
            ->join("tb_sender s on s.id=o.sender_id")
            ->join("tb_country co on co.id=r.country_id")
            ->join("tb_order_goods og on og.order_id=o.id")
            ->join("tb_goods g on g.id=og.goods_id")
            ->join("tb_users u on u.id=o.users_id")
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order("o.ord_time DESC")->select();
        $this->assign("page",$show);
        $this->assign("data",$orderdata);
        $this->display();
    }
    public function ship_clerk(){
        $count      = M("order")->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('prev' , '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('first', '第一页');
        $Page->setConfig('last', '尾页');
        $show       = $Page->show();// 分页显示输出
        $orderdata = M("order")->alias("o")
            ->join("tb_channel c on c.id=o.channel_id")
            ->join("tb_recipients r on r.id=o.recipients_id")
            ->join("tb_sender s on s.id=o.sender_id")
            ->join("tb_country co on co.id=r.country_id")
            ->join("tb_order_goods og on og.order_id=o.id")
            ->join("tb_goods g on g.id=og.goods_id")
            ->join("tb_users u on u.id=o.users_id")
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order("o.ord_time DESC")->select();
        $this->assign("page",$show);
        $this->assign("data",$orderdata);
        $this->display();
    }
    public function goods_clerk(){
        $count      = M("order")->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('prev' , '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('first', '第一页');
        $Page->setConfig('last', '尾页');
        $show       = $Page->show();// 分页显示输出
        $orderdata = M("order")->alias("o")
            ->join("tb_channel c on c.id=o.channel_id")
            ->join("tb_recipients r on r.id=o.recipients_id")
            ->join("tb_sender s on s.id=o.sender_id")
            ->join("tb_country co on co.id=r.country_id")
            ->join("tb_order_goods og on og.order_id=o.id")
            ->join("tb_goods g on g.id=og.goods_id")
            ->join("tb_users u on u.id=o.users_id")
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order("o.ord_time DESC")->select();
        $this->assign("page",$show);
        $this->assign("data",$orderdata);
        $this->display();
    }
    public function finance(){
        $count      = M("order")->count();// 查询满足要求的总记录数
        $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('prev' , '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('first', '第一页');
        $Page->setConfig('last', '尾页');
        $show       = $Page->show();// 分页显示输出
        $orderdata = M("order")->alias("o")
            ->join("tb_channel c on c.id=o.channel_id")
            ->join("tb_recipients r on r.id=o.recipients_id")
            ->join("tb_sender s on s.id=o.sender_id")
            ->join("tb_country co on co.id=r.country_id")
            ->join("tb_order_goods og on og.order_id=o.id")
            ->join("tb_goods g on g.id=og.goods_id")
            ->join("tb_users u on u.id=o.users_id")
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order("o.ord_time DESC")->select();
        $this->assign("page",$show);
        $this->assign("data",$orderdata);
        $this->display();
    }
    //搜索
    public function search(){
        $post= I("post.");
        if(!empty($post["way"])){
            $orderdata = M("order")->alias("o")
                ->join("tb_channel c on c.id=o.channel_id")
                ->join("tb_recipients r on r.id=o.recipients_id")
                ->join("tb_sender s on s.id=o.sender_id")
                ->join("tb_country co on co.id=r.country_id")
                ->join("tb_order_goods og on og.order_id=o.id")
                ->join("tb_goods g on g.id=og.goods_id")
                ->join("tb_users u on u.id=o.users_id")
                ->order("o.ord_time DESC")
                ->where("o.way={$post['way']}")->select();
            $this->assign("data",$orderdata);
            $this->display("Order:index");
        }elseif(!empty($post['date1'])){
            $orderdata = M("order")->alias("o")
                ->join("tb_channel c on c.id=o.channel_id")
                ->join("tb_recipients r on r.id=o.recipients_id")
                ->join("tb_sender s on s.id=o.sender_id")
                ->join("tb_country co on co.id=r.country_id")
                ->join("tb_order_goods og on og.order_id=o.id")
                ->join("tb_goods g on g.id=og.goods_id")
                ->join("tb_users u on u.id=o.users_id")
                ->order("o.ord_time DESC")
                ->where("o.ord_time like '{$post['date1']}%'")->select();
            $this->assign("data",$orderdata);
            $this->display("Order:index");
        }else{
            $count      = M("order")->count();// 查询满足要求的总记录数
            $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $Page->setConfig('prev' , '上一页');
            $Page->setConfig('next', '下一页');
            $Page->setConfig('first', '第一页');
            $Page->setConfig('last', '尾页');
            $show       = $Page->show();// 分页显示输出
            $orderdata = M("order")->alias("o")
                ->join("tb_channel c on c.id=o.channel_id")
                ->join("tb_recipients r on r.id=o.recipients_id")
                ->join("tb_sender s on s.id=o.sender_id")
                ->join("tb_country co on co.id=r.country_id")
                ->join("tb_order_goods og on og.order_id=o.id")
                ->join("tb_goods g on g.id=og.goods_id")
                ->join("tb_users u on u.id=o.users_id")
                ->limit($Page->firstRow.','.$Page->listRows)
                ->order("o.ord_status,o.cancel asc,o.ord_time desc")->where("o.cancel=0")->select();
            $this->assign("page",$show);
            $this->assign("data",$orderdata);
            $this->display("Order:index");
        }
    }
    //客服
    public function cu_search(){
        $post= I("post.");
        if(!empty($post["way"])){
            $orderdata = M("order")->alias("o")
                ->join("tb_channel c on c.id=o.channel_id")
                ->join("tb_recipients r on r.id=o.recipients_id")
                ->join("tb_sender s on s.id=o.sender_id")
                ->join("tb_country co on co.id=r.country_id")
                ->join("tb_order_goods og on og.order_id=o.id")
                ->join("tb_goods g on g.id=og.goods_id")
                ->join("tb_users u on u.id=o.users_id")
                ->order("o.ord_time DESC")
                ->where("o.way={$post['way']}")->select();
            $this->assign("data",$orderdata);
            $this->display("Order:custom_service");
        }elseif(!empty($post['date1'])){
            $orderdata = M("order")->alias("o")
                ->join("tb_channel c on c.id=o.channel_id")
                ->join("tb_recipients r on r.id=o.recipients_id")
                ->join("tb_sender s on s.id=o.sender_id")
                ->join("tb_country co on co.id=r.country_id")
                ->join("tb_order_goods og on og.order_id=o.id")
                ->join("tb_goods g on g.id=og.goods_id")
                ->join("tb_users u on u.id=o.users_id")
                ->order("o.ord_time DESC")
                ->where("o.ord_time like '{$post['date1']}%'")->select();
            $this->assign("data",$orderdata);
            $this->display("Order:custom_service");
        }else{
            $count      = M("order")->count();// 查询满足要求的总记录数
            $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $Page->setConfig('prev' , '上一页');
            $Page->setConfig('next', '下一页');
            $Page->setConfig('first', '第一页');
            $Page->setConfig('last', '尾页');
            $show       = $Page->show();// 分页显示输出
            $orderdata = M("order")->alias("o")
                ->join("tb_channel c on c.id=o.channel_id")
                ->join("tb_recipients r on r.id=o.recipients_id")
                ->join("tb_sender s on s.id=o.sender_id")
                ->join("tb_country co on co.id=r.country_id")
                ->join("tb_order_goods og on og.order_id=o.id")
                ->join("tb_goods g on g.id=og.goods_id")
                ->join("tb_users u on u.id=o.users_id")
                ->limit($Page->firstRow.','.$Page->listRows)
                ->order("o.ord_status,o.cancel asc,o.ord_time desc")->where("o.cancel=0")->select();
            $this->assign("page",$show);
            $this->assign("data",$orderdata);
            $this->display("Order:custom_service");
        }
    }
    //收货
    public function go_search(){
        $post= I("post.");
        if(!empty($post["way"])){
            $orderdata = M("order")->alias("o")
                ->join("tb_channel c on c.id=o.channel_id")
                ->join("tb_recipients r on r.id=o.recipients_id")
                ->join("tb_sender s on s.id=o.sender_id")
                ->join("tb_country co on co.id=r.country_id")
                ->join("tb_order_goods og on og.order_id=o.id")
                ->join("tb_goods g on g.id=og.goods_id")
                ->join("tb_users u on u.id=o.users_id")
                ->order("o.ord_time DESC")
                ->where("o.way={$post['way']}")->select();
            $this->assign("data",$orderdata);
            $this->display("Order:goods_clerk");
        }elseif(!empty($post['date1'])){
            $orderdata = M("order")->alias("o")
                ->join("tb_channel c on c.id=o.channel_id")
                ->join("tb_recipients r on r.id=o.recipients_id")
                ->join("tb_sender s on s.id=o.sender_id")
                ->join("tb_country co on co.id=r.country_id")
                ->join("tb_order_goods og on og.order_id=o.id")
                ->join("tb_goods g on g.id=og.goods_id")
                ->join("tb_users u on u.id=o.users_id")
                ->order("o.ord_time DESC")
                ->where("o.ord_time like '{$post['date1']}%'")->select();
            $this->assign("data",$orderdata);
            $this->display("Order:goods_clerk");
        }else{
            $count      = M("order")->count();// 查询满足要求的总记录数
            $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $Page->setConfig('prev' , '上一页');
            $Page->setConfig('next', '下一页');
            $Page->setConfig('first', '第一页');
            $Page->setConfig('last', '尾页');
            $show       = $Page->show();// 分页显示输出
            $orderdata = M("order")->alias("o")
                ->join("tb_channel c on c.id=o.channel_id")
                ->join("tb_recipients r on r.id=o.recipients_id")
                ->join("tb_sender s on s.id=o.sender_id")
                ->join("tb_country co on co.id=r.country_id")
                ->join("tb_order_goods og on og.order_id=o.id")
                ->join("tb_goods g on g.id=og.goods_id")
                ->join("tb_users u on u.id=o.users_id")
                ->limit($Page->firstRow.','.$Page->listRows)
                ->order("o.ord_status,o.cancel asc,o.ord_time desc")->where("o.cancel=0")->select();
            $this->assign("page",$show);
            $this->assign("data",$orderdata);
            $this->display("Order:goods_clerk");
        }
    }
    //出货
    public function ship_search(){
        $post= I("post.");
        if(!empty($post["way"])){
            $orderdata = M("order")->alias("o")
                ->join("tb_channel c on c.id=o.channel_id")
                ->join("tb_recipients r on r.id=o.recipients_id")
                ->join("tb_sender s on s.id=o.sender_id")
                ->join("tb_country co on co.id=r.country_id")
                ->join("tb_order_goods og on og.order_id=o.id")
                ->join("tb_goods g on g.id=og.goods_id")
                ->join("tb_users u on u.id=o.users_id")
                ->order("o.ord_time DESC")
                ->where("o.way={$post['way']}")->select();
            $this->assign("data",$orderdata);
            $this->display("Order:ship_clerk");
        }elseif(!empty($post['date1'])){
            $orderdata = M("order")->alias("o")
                ->join("tb_channel c on c.id=o.channel_id")
                ->join("tb_recipients r on r.id=o.recipients_id")
                ->join("tb_sender s on s.id=o.sender_id")
                ->join("tb_country co on co.id=r.country_id")
                ->join("tb_order_goods og on og.order_id=o.id")
                ->join("tb_goods g on g.id=og.goods_id")
                ->join("tb_users u on u.id=o.users_id")
                ->order("o.ord_time DESC")
                ->where("o.ord_time like '{$post['date1']}%'")->select();
            $this->assign("data",$orderdata);
            $this->display("Order:ship_clerk");
        }else{
            $count      = M("order")->count();// 查询满足要求的总记录数
            $Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
            $Page->setConfig('prev' , '上一页');
            $Page->setConfig('next', '下一页');
            $Page->setConfig('first', '第一页');
            $Page->setConfig('last', '尾页');
            $show       = $Page->show();// 分页显示输出
            $orderdata = M("order")->alias("o")
                ->join("tb_channel c on c.id=o.channel_id")
                ->join("tb_recipients r on r.id=o.recipients_id")
                ->join("tb_sender s on s.id=o.sender_id")
                ->join("tb_country co on co.id=r.country_id")
                ->join("tb_order_goods og on og.order_id=o.id")
                ->join("tb_goods g on g.id=og.goods_id")
                ->join("tb_users u on u.id=o.users_id")
                ->limit($Page->firstRow.','.$Page->listRows)
                ->order("o.ord_status,o.cancel asc,o.ord_time desc")->where("o.cancel=0")->select();
            $this->assign("page",$show);
            $this->assign("data",$orderdata);
            $this->display("Order:ship_clerk");
        }
    }
    //打印页
    public function ord_print(){
        $data = M("order")->alias("o")
            ->join("tb_channel q on q.id=o.channel_id")
            ->join("tb_recipients r on r.id=o.recipients_id")
            ->join("tb_country c on c.id=r.country_id")
            ->join("tb_sender s on s.id=o.sender_id")
            ->where("o.id={$_GET['nid']}")->select();
        $goods=M("goods")->alias("g")
            ->join("tb_order_goods og on g.id=og.goods_id")
            ->where("og.order_id={$_GET['nid']}")->select();
        $this->assign("goods",$goods);
        $this->assign("data",$data);
        $this->display();
}
    //转运
    public function transit(){
        if(!IS_POST){
            $this->display();
        }else{
            $post = I('post.');
        }
    }
    //信息修改
    public function edit(){
        if(IS_POST) {
            $id=$_GET['id'];
            if(empty($id)) {
                $this->error('异常操作！',__MODULE__.'/order/index');
            }else{
                $data=I("post.");
                $test=M("order")->data($data)->where("id={$id}")->save();
                if($test) {
                    $this->success('操作成功！',__MODULE__.'/order/index');
                } else {
                    $this->error("操作失败");
                }

            }
        }else{
            $id=$_GET["nid"];
            if(empty($id)) {
                $this->error('异常操作！',__MODULE__.'/order/index');
            }else{
                $arr= M("order")->alias("o")
                    ->join("tb_channel c on c.id=o.channel_id")
                    ->join("tb_recipients r on r.id=o.recipients_id")
                    ->join("tb_sender s on s.id=o.sender_id")
                    ->join("tb_country co on co.id=r.country_id")
                    ->join("tb_order_goods og on og.order_id=o.id")
                    ->join("tb_goods g on g.id=og.goods_id")
                    ->join("tb_users u on u.id=o.users_id")
                    ->where("o.id={$id}")
                    ->select();
                $this->assign("arr",$arr);
                $this->display();

            }
        }
    }
    //取消运单
    public function del(){
        $nid = intval($_GET['nid']);
        if(empty($nid)) {
            $this->error('异常操作！',__MODULE__.'/order/index');
        }
        if(M("order")->where("id = {$nid}")->delete() !== fasle) {
            $this->success('操作成功！',__MODULE__.'/order/index');
        } else {
            $this->error('操作失败！',__MODULE__.'/order/index');
        }
    }
    public function affirm(){
        $bol = M("order")->data($_GET)->where("id={$_GET['nid']}")->save();
        if($bol){
            echo 1;
        }else{
            echo 0;
        }
    }
    //双击编辑事件
    public function dbl_click(){
        $arr[$_GET["fie"]]=$_GET["value"];
        $data=M("order")->alias("o")
            ->join("tb_channel c on c.id=o.channel_id")
            ->join("tb_recipients r on r.id=o.recipients_id")
            ->join("tb_sender s on s.id=o.sender_id")
            ->join("tb_country co on co.id=r.country_id")
            ->join("tb_order_goods og on og.order_id=o.id")
            ->join("tb_goods g on g.id=og.goods_id")
            ->where("o.id=".$_GET['nid'])->select();
        if($_GET["fie"]=="weight"){
            $scheme = M("weig_add")->alias("w")
                ->join("tb_starting s on s.id=w.start_id")
                ->join("tb_partition_country pc on pc.id=s.pcid")
                ->join("tb_channel_partition cp on cp.id=s.cpid")
                ->join("tb_partition p on p.id=pc.partition_id and p.id=cp.partition_id ")
                ->join("tb_channel q on q.id=cp.channel_id")
                ->join("tb_country c on c.id=pc.country_id")
                ->where("c.id={$data[0]['country_id']} and q.id={$data[0]['channel_id']}")
                ->select();
            //进行重量段判断，算出价格
            foreach($scheme as $value) {
                if ($_GET['value'] >= $value['start_weight']) {
                    if ($value['stage_min_weig'] <= $_GET['value'] && $value['stage_max_weig'] >= $_GET['value']) {
                        if(($_GET['value'] - $value['start_weight']) % $value['add_weig']!=0){
                            $star = ($_GET['value'] - $value['start_weight']) / $value['add_weig'];
                            $yun = $star * $value['add_weig_price'] + $value['start_fare'];
                        }else{
                            $yun = ($_GET['value'] - $value['start_weight']) / $value['add_weig'] * $value['add_weig_price'] + $value['start_fare'];
                        }
                        $arr['money'] = $yun +$yun * $value["fuel"]/100;
                    }
                }elseif($value['stage_min_weig'] <= ceil($_GET['value']) && $value['stage_max_weig'] >= ceil($_GET['value'])){
                    if(($_GET['value'] - $value['start_weight']) % $value['add_weig']!=0){
                        $star = ($_GET['value'] - $value['start_weight']) / $value['add_weig'];
                        $yun = $star * $value['add_weig_price'] + $value['start_fare'];
                    }else{
                        $yun = ($_GET['value'] - $value['start_weight']) / $value['add_weig'] * $value['add_weig_price'] + $value['start_fare'];
                    }
                    $arr['money'] = $yun +$yun * $value["fuel"]/100;
                }
            }
        }
        if(array_diff($arr,$data)){
            $bol=M("order")->data($arr)->where("id=".$_GET['nid'])->save();
        }else{
            $good=M("goods")->select();
            if(array_diff($arr,$good)){
                $bol=M("order")->data($arr)->where("id=".$data['goods_id'])->save();
            }
        }
        echo json_encode($bol);

    }
    public function _empty(){
        $this->error("404");
    }
}